#import "ARLColorUtils.h"
#import "ARLMathUtils.h"

@implementation ARLColorUtils
static NSArray* metroColor;
+(void)initialize{
    metroColor = @[@"252525",@"006AC1",@"691BB8",@"F4B300",@"78BA00",@"2673EC",@"AE113D",@"632F00",@"2E1700",@"199900",
                   @"004A00",@"00C13F",@"159924",@"FF981D",@"1B58B8",@"56C5FF",@"E1B700",@"FF76BC",@"B81B1B",@"00A4A4"];
}

+ (UIColor*)colorWithHexString:(NSString *)hexStr {
    CGFloat r, g, b, a;
    if (hexStrToRGBA(hexStr, &r, &g, &b, &a)) {
        return [UIColor colorWithRed:r green:g blue:b alpha:a];
    }
    return nil;
}
//
+ (NSString *)hexStringColor:(UIColor*)uiColor {
    return [self colorWithHexString:uiColor withAlpha:NO];
}

+ (NSString *)hexStringColorWithAlpha:(UIColor*)uiColor {
    return [self colorWithHexString:uiColor withAlpha:YES];
}

+ (NSString *)colorWithHexString:(UIColor*)uiColor withAlpha:(BOOL)withAlpha {
    CGColorRef color = uiColor.CGColor;
    size_t count = CGColorGetNumberOfComponents(color);
    const CGFloat *components = CGColorGetComponents(color);
    static NSString *stringFormat = @"%02x%02x%02x";
    NSString *hex = nil;
    if (count == 2) {
        NSUInteger white = (NSUInteger)(components[0] * 255.0f);
        hex = [NSString stringWithFormat:stringFormat, white, white, white];
    } else if (count == 4) {
        hex = [NSString stringWithFormat:stringFormat,
               (NSUInteger)(components[0] * 255.0f),
               (NSUInteger)(components[1] * 255.0f),
               (NSUInteger)(components[2] * 255.0f)];
    }
    
    if (hex && withAlpha) {
        hex = [hex stringByAppendingFormat:@"%02lx",
               (unsigned long)(CGColorGetAlpha(uiColor.CGColor) * 255.0 + 0.5)];
    }
    return hex;
}

+ (NSString *)trimString:(NSString*)str {
    NSCharacterSet *set = [NSCharacterSet whitespaceAndNewlineCharacterSet];
    return [str stringByTrimmingCharactersInSet:set];
}


static inline NSUInteger hexStrToInt(NSString *str) {
    uint32_t result = 0;
    sscanf([str UTF8String], "%X", &result);
    return result;
}

static BOOL hexStrToRGBA(NSString *str,
                         CGFloat *r, CGFloat *g, CGFloat *b, CGFloat *a) {
    str = [[ARLColorUtils trimString:str] uppercaseString];
    if ([str hasPrefix:@"#"]) {
        str = [str substringFromIndex:1];
    } else if ([str hasPrefix:@"0X"]) {
        str = [str substringFromIndex:2];
    }
    
    NSUInteger length = [str length];
    //         RGB            RGBA          RRGGBB        RRGGBBAA
    if (length != 3 && length != 4 && length != 6 && length != 8) {
        return NO;
    }
    
    //RGB,RGBA,RRGGBB,RRGGBBAA
    if (length < 5) {
        *r = hexStrToInt([str substringWithRange:NSMakeRange(0, 1)]) / 255.0f;
        *g = hexStrToInt([str substringWithRange:NSMakeRange(1, 1)]) / 255.0f;
        *b = hexStrToInt([str substringWithRange:NSMakeRange(2, 1)]) / 255.0f;
        if (length == 4)  *a = hexStrToInt([str substringWithRange:NSMakeRange(3, 1)]) / 255.0f;
        else *a = 1;
    } else {
        *r = hexStrToInt([str substringWithRange:NSMakeRange(0, 2)]) / 255.0f;
        *g = hexStrToInt([str substringWithRange:NSMakeRange(2, 2)]) / 255.0f;
        *b = hexStrToInt([str substringWithRange:NSMakeRange(4, 2)]) / 255.0f;
        if (length == 8) *a = hexStrToInt([str substringWithRange:NSMakeRange(6, 2)]) / 255.0f;
        else *a = 1;
    }
    return YES;
}

+(UIColor*)randomMetroUIColor{
    NSInteger randIdx = [ARLMathUtils getRandomNumber:0 to:(int)metroColor.count];
    return [self colorWithHexString:metroColor[randIdx]];
}

@end
